package TEMAnalysis;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class Control {
	
	int[][] projection = new int[256][256];
	BufferedImage image;
	
	public double F220(double x, double y, double z){
		
		double pi = Math.PI;
		
		return Math.cos(4*x*pi)*Math.cos(4*y*pi) + Math.cos(4*pi*y)*Math.cos(4*z*pi) + Math.cos(4*pi*x)*Math.cos(4*z*pi);
	}

	public double F211(double x, double y, double z){
		
		double pi = Math.PI;
		
		return Math.sin(4*pi*x)*Math.sin(2*pi*z)*Math.cos(2*pi*y) + Math.sin(4*y*pi)*Math.sin(2*x*pi)*Math.cos(2*z*pi) + Math.sin(4*pi*z)*Math.sin(2*pi*y)*Math.cos(2*pi*x);
	}
	
	public void getProjectionPlane(int h, int k, int l, double f, double s){
		
		double nx = 0, ny = 0, nz = 0;
		double mx = 0, my = 0, mz = 0;
		
		double t = -1 + (2*f/100);
		
		if(h == 0){
			nx = 1;
			if(k == 0){
				my = 1;
			}
			else if(l == 0){
				mz = 1;
			}
			else{
				mz = 1;///Math.sqrt(1+l*l/(k*k));
				my = -l/k;//(k*Math.sqrt(1+l*l/(k*k)));
			}
		}
		else if(k == 0){
			ny = 1;
			if(h == 0){
				mx = 1;
			}
			else if(l == 0){
				mz = 1;
			}
			else{
				mz = 1;///Math.sqrt(1+l*l/(h*h));
				mx = (double)-l/h;//(h*Math.sqrt(1+l*l/(h*h)));
			}
		}
		else if(l == 0){
			nz = 1;
			if(h == 0){
				mx = 0;
			}
			else if(k == 0){
				my = 0;
			}
			else{
				my = 1;///Math.sqrt(1+k*k/(h*h));
				mx = (double)-k/h;//(h*Math.sqrt(1+k*k/(h*h)));
			}
		}
		else{
			nx = (double)-k/h;
			mx = k*nz-l*ny;
			my = l*nx-h*nz;
			mz = h*ny-k*nx;
		}
		
		double increment = (double)1/256;
		
		int counter = 0; 
		double ux = (double)h;///Math.sqrt(h*h + k*k + l*l);
		double uy = (double)k;///Math.sqrt(h*h + k*k + l*l);
		double uz = (double)l;///Math.sqrt(h*h + k*k + l*l);
		

		System.out.println("n-vector: " + nx + ", " + ny + ", " + nz);
		System.out.println("m-vector: " + mx + ", " + my + ", " + mz);
		System.out.println("u-vector: " + ux + ", " + uy + ", " + uz);
		
		
		double gn, gm, gu, x, y, z;
		double calculatedT;
		
		int sum = 0;
		
		while(counter < 256*256*256){
			
			gn = increment*((counter%(256*256))/256);
			gm = increment*(counter/(256*256));
			gu = increment*((counter%(256*256))%256);

			x = gn*nx + gm*mx + gu*ux;
			y = gn*ny + gm*my + gu*uy;
			z = gn*nz + gm*mz + gu*uz;
			
			calculatedT = s*F211(x, y, z) - (1-s)*F220(x, y, z);
			
			if((counter%(256*256))%256 == 255){
				projection[(counter%(256*256))/256][counter/(256*256)] = sum;
				sum = 0;
			}
			else if(calculatedT <= t){
				sum++;
			}
			
			counter++;
		}
	}
	
	public void drawProjection(){

		int length = 1024;
		
		image = new BufferedImage(length, length, BufferedImage.TYPE_INT_RGB);

		for(int count = 0; count < length; count = count + 1){
			for(int count1 = 0; count1 < length; count1 = count1 + 1){
				image.setRGB(count1, count, (new Color(projection[count1%256][count%256], projection[count1%256][count%256], projection[count1%256][count%256])).getRGB());
			}
		}
	}
	
}
